/*
 * cache.c
 *
 *  Created on: Feb 20, 2012
 *      Author: albert
 */

#include "cache.h"
#include "time.h"
extern unsigned long int ** cache_buffer;
extern unsigned int cache_size;
extern int *sector_index_buffer;
extern unsigned int *frequency_buffer;
extern unsigned int lowest_frequency;
extern unsigned int lowest_frequency_index;
#define ENTRY_SECTOR_SIZE 512

unsigned int cache_enable = 0;

int cache_contains(int sector_no)
{
	if(cache_enable == 1)
	{
	int i;
	int result = -1;
	for(i=0; i<cache_size; i++)
	{
		if(sector_no == sector_index_buffer[i])
		{
			result = i;
			break;
		}
	}
	return result;
	}
	else
		return -1;
}

int copy_cache_to(int index, unsigned long int * buf)
{
	int i;
	buf = cache_buffer[index];
	return 0;
}

int cache_update(int sector_no, unsigned long int * buf)
{
	if(cache_enable == 1)
	{
	//srand(time(NULL));
	int index = rand() % (cache_size) + 0;//printf("replace %d\n", index);
	sector_index_buffer[index] = sector_no;
	memcpy(cache_buffer[index], buf, sizeof(ENTRY_SECTOR_SIZE));
	}
	return 0;
}
